/** 
 * ================================================================================
 * 		Project		: Kompas Bola
 * 		File		: KlasemenActivity.java
 * 		Description	: Klasemen Stat Bola Screen
 * 		Called		: MainActivity.java
 * ================================================================================
 * */
package com.kompas.bola.application;

import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;
import com.actionbarsherlock.app.ActionBar;
import com.kompas.bola.application.R;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Fields;
import com.kompas.bola.application.analytics.FlurryUtil;
import com.kompas.bola.application.analytics.GAnalyticsUtil;
import com.kompas.bola.application.dao.Dao;
import com.kompas.bola.application.fragment.KlasemenFragment;
import com.kompas.bola.application.fragment.MatchFragment;
import com.kompas.bola.application.fragment.TopSkorFragment;
import com.kompas.bola.application.model.Content;
import com.kompas.bola.application.ui.AbstractTabStackNavigation;
import com.kompas.bola.application.utils.Constants;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
/** =========================================================================== */
public class KlasemenActivity extends AbstractTabStackNavigation {
	private AsyncTask<String, Void, List<Content>> klasemenTask;
	private HashMap<String, String> value;
	private ProgressDialog pDialog;
	private static Menu _menuInstance;
	/** ======================================================================= */	
	public void addTab(final String title, final Fragment fragment) {
	    final ActionBar.Tab tab 					= getSupportActionBar().newTab();
	    tab.setText(title);
	    addTab(tab, fragment);	
	  }
	/** ======================================================================= */	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		_menuInstance = menu;
		
		menu.add(getString(R.string.refresh))
				.setIcon(R.drawable.ic_menu_back).setVisible(true)
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
		return true;
	}
	/** ======================================================================= */	
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getTitle().equals(getString(R.string.refresh))) {
            finish();
			overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
            return true;
        }
        return false;
    }
	/** ======================================================================= */	
	@Override
	protected void createTabs() {
		/** 
		 * START GOOGLE ANALYTICS */
		value										= new HashMap<String, String>();
		value.put(Fields.HIT_TYPE, "appview");
		value.put(Fields.SCREEN_NAME, Constants.KLASEMEN_SCREEN);
		GAnalyticsUtil.startSessionWithGA(this,value);
		
		overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left);

		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);
		getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff")));
		getActionBar().setLogo(getResources().getDrawable(R.drawable.logo));
		getActionBar().setTitle(Constants.KLASEMEN_SCREEN);

        Intent in 									= getIntent(); 
		String liga									= in.getStringExtra(Constants.LIGA);
		
		/** 
		 * Load Jadwal Task */
		new LoadKlasemen().execute(liga);
	}
	private class LoadKlasemen extends AsyncTask<String, Void, List<Content>>{
		protected void onPreExecute(){
			pDialog									= new ProgressDialog(KlasemenActivity.this);
			pDialog.setMessage("loading...");
			pDialog.setOnCancelListener(new android.content.DialogInterface.OnCancelListener() {
				
				public void onCancel(DialogInterface dialog) {
					// TODO Auto-generated method stub
					pDialog.hide();
					Toast.makeText(KlasemenActivity.this, Constants.PROCESS_CANCEL, Toast.LENGTH_SHORT).show();
					cancel(true);
				}
			});
			pDialog.setCancelable(true);
			pDialog.show();
		}

		@Override
		protected List<Content> doInBackground(String... params) {
			// TODO Auto-generated method stub
			String liga								= params[0];
			return Dao.getKlasemenContent(liga);
		}

		protected void onPostExecute(
				List<Content> result){
			if (isCancelled())
				return;		
			
			try {
				if(result.size() > 0){
					
					try {
						/** 
						 * TAB KLASEMEN */
						addTab(Constants.KLASEMEN,new KlasemenFragment(result));		
						/** 
						 * TAB HASIL PERTANDINGAN */
						addTab(Constants.HASIL_PERTANDINGAN,new MatchFragment(result));		
						/** 
						 * TAB TOPSKOR */
						addTab(Constants.TOP_SKOR,new TopSkorFragment(result));
					} catch (Exception e) {
						// TODO Auto-generated catch block
						cancel(true);
					}
					
					pDialog.cancel();
				}else{	
					Toast.makeText(KlasemenActivity.this, Constants.NO_CONNECTION, Toast.LENGTH_SHORT).show();
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}									
		}
	}
	/** =========================================================================== */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
        	Intent main								= new Intent(KlasemenActivity.this, MainActivity.class);
            startActivity(main);
			overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }	
	/** =========================== s : Google Analytics ========================== */
	@Override
	public void onStart() {
		super.onStart();		
		
		EasyTracker.getInstance(this).activityStart(this);  // Add this method.		
	    FlurryUtil.startSessionWithFlurry(this, Constants.KLASEMEN_SCREEN);
	}
	/** =========================================================================== */
	@Override
	public void onStop() {
		super.onStop();

		/** 
		 * Load Jadwal Task */
		new LoadKlasemen().cancel(true);
		
	    EasyTracker.getInstance(this).activityStop(this);  // Add this method.
	    FlurryUtil.endSessionWithFlurry(this,  Constants.KLASEMEN_SCREEN);
	}
	/** =========================== e : Google Analytics ========================== */
}
